package com.topvision.elementanim.hualang;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.topvision.elementanim.R;

import java.io.File;
import java.util.List;

/**
 * Author: lucy
 * Comment: //TODO
 * Date: 2017-09-28 15:20
 */

public class MyAdapter extends PagerAdapter implements View.OnClickListener {
    private final LayoutInflater mInflater;
    Context context;
    List<File> imageRes;
    ViewPager viewPager;
    ItemClickedInterface itemClickedInterface;

    public MyAdapter(Context context, ViewPager viewPager, List<File> imageRes, ItemClickedInterface itemClickedInterface) {
        this.context = context;
        this.viewPager = viewPager;
        this.imageRes = imageRes;
        this.itemClickedInterface = itemClickedInterface;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return imageRes.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return object == view;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = mInflater.inflate(R.layout.item_image, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        FrameLayout player = (FrameLayout) view.findViewById(R.id.iv_player);
        File file = imageRes.get(position);
        if (file.getName().endsWith(".mp4"))
            player.setVisibility(View.VISIBLE);
        else
            player.setVisibility(View.GONE);
        Glide.with(context).load(file).into(imageView);
        imageView.setOnClickListener(this);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(((View) object));
    }

    @Override
    public void onClick(View view) {
        if (itemClickedInterface!=null) {
            itemClickedInterface.onItemClicked(view);
        }
    }
}
